//
//  RankingListViewController.m
//  WuLi
//
//  Created by Gollum on 16/8/30.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "RankingListViewController.h"
#import "RankingTableViewCell.h"
#import "RankDetailViewController.h"
#import "RankModel.h"
#import "RankData.h"
#import "RankAttached.h"
#import "RankNewscoverinfo.h"
#import "RankNewsimageinfolist.h"
#import "RankParamap.h"
#import "NetworkTool.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#define kRankingTableViewCellReuse @"RankingReuse"
@interface RankingListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)RankModel *rankData;
//容器
@property (nonatomic,strong)NSMutableArray *containerArray;
@end

@implementation RankingListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _rankData = [[RankModel alloc] init];
    _containerArray = [NSMutableArray array];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 112) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.rowHeight = 100;
    [_tableView registerNib:[UINib nibWithNibName:@"RankingTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kRankingTableViewCellReuse];
    
    //    刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self handleData];
            
            [_tableView.mj_header endRefreshing];
            
        });
    }];
    [self.tableView.mj_header beginRefreshing];

}


- (void)handleData{

    NSString *urlString = @"http://211.151.51.100/v2/message/rank/list?timestamp=0";
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"api.5wuli.com"};
    
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        //        NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",result);
        _rankData = [RankModel modelWithDic:result];
        
        [self.containerArray addObject:_rankData];
        
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 130;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _rankData.data.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    RankingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kRankingTableViewCellReuse];
    cell.title.text = _rankData.data[indexPath.row].title;
    cell.origin.text = _rankData.data[indexPath.row].origin;
    cell.score.text = [NSString stringWithFormat:@"%ld",_rankData.data[indexPath.row].score];
    cell.commentTotalCount.text = [NSString stringWithFormat:@"%ld",_rankData.data[indexPath.row].commentTotalCount];

    NSURL *url = [NSURL URLWithString:_rankData.data[indexPath.row].newsImageInfoList[0].url];
    [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
    NSString *str = @"TOP";
    NSString *index = [NSString stringWithFormat:@"%ld",indexPath.row + 1  ];
    cell.Top.text = [str stringByAppendingString:index];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    RankDetailViewController *detail = [[RankDetailViewController alloc] init];
    detail.detailString = _rankData.data[indexPath.row].detailLink;
    [self presentViewController:detail animated:YES completion:^{
        
    }];


}


//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    CATransform3D rotation;
//    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
//    rotation.m34 = 1.0/ -600;
//    
//    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//    cell.layer.shadowOffset = CGSizeMake(10, 10);
//    cell.alpha = 0;
//    cell.layer.transform = rotation;
//    cell.layer.anchorPoint = CGPointMake(0, 0.5);
//    
//    
//    [UIView beginAnimations:@"rotation" context:NULL];
//    [UIView setAnimationDuration:0.8];
//    cell.layer.transform = CATransform3DIdentity;
//    cell.alpha = 1;
//    cell.layer.shadowOffset = CGSizeMake(0, 0);
//    [UIView commitAnimations];
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
